package mymac.com.weather;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity {
    private TextView 城市,温度,气候,气温范围,日期,周日期;
    private GridView gridView;
    private Button 切换城市,未来几日;
    private List<Map<String , Object>> gridDatas;
    private SimpleAdapter simpleAdapter;
    private int[] gridImage = {R.drawable.ziwaixian,R.drawable.lvyou,R.drawable.fengxiang,R.drawable.shidu,R.drawable.zhishu,R.drawable.shushidu,R.drawable.washbus,R.drawable.sport,R.drawable.jianyi};
    private String[] gridImageText = {"紫外线","旅游","风向","湿度","穿衣指数","舒适度","洗车","运动","穿衣建议"};
    private String[] gridItemData;
    private AlertDialog.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        城市 = (TextView) findViewById(R.id.城市);
        温度 = (TextView) findViewById(R.id.温度);
        气候 = (TextView) findViewById(R.id.气候);
        气温范围 = (TextView) findViewById(R.id.气温范围);
        日期 = (TextView) findViewById(R.id.日期);
        周日期 = (TextView) findViewById(R.id.周日期);
        切换城市 = (Button) findViewById(R.id.切换城市);
        未来几日 = (Button) findViewById(R.id.未来几日);
        gridView = (GridView) findViewById(R.id.格子布局);

        城市.setText(Today.getInstance().getCity());
        温度.setText(Sk.getInstance().getTemp());
        气候.setText(Today.getInstance().getWeather());
        气温范围.setText(Today.getInstance().getTemperature());
        日期.setText(Today.getInstance().getDate_y());
        周日期.setText(Today.getInstance().getWeek());

        gridDatas = new ArrayList<Map<String, Object>>();
        gridItemData = new String[]{Today.getInstance().getUv_index(), Today.getInstance().getTravel_index(), Today.getInstance().getWind(), Sk.getInstance().getHumidity(), Today.getInstance().getDressing_index(), Today.getInstance().getComfort_index(), Today.getInstance().getWash_index(), Today.getInstance().getExercise_index(), Today.getInstance().getDressing_advice()};
        getGirdDatas();
        String[] form = {"gridImage","gridImageText","gridItemData"};
        int[] to = {R.id.item_imageView,R.id.item_textView,R.id.item_textContent};
        simpleAdapter = new SimpleAdapter(this,gridDatas,R.layout.activity_grid_item,form,to);
        gridView.setAdapter(simpleAdapter);

        未来几日.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,FutureActivity.class);
                startActivity(intent);
            }
        });

        切换城市.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,StartActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    private List<Map<String,Object>> getGirdDatas() {
        for (int i=0; i<gridImage.length; i++){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("gridImage",gridImage[i]);
            map.put("gridImageText",gridImageText[i]);
            map.put("gridItemData",gridItemData[i]);
            gridDatas.add(map);
        }
        return gridDatas;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN){
            builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("退出");
            builder.setMessage("是否天气预报");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });
            builder.setNegativeButton("取消",null);
            builder.show();
        }
        return super.onKeyDown(keyCode, event);
    }


}
